import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//通过这个Servlet生成一个主页
@WebServlet("/index")
public class IndexServlet extends HelloServlet{
    @Override
    //重定向的请求是Get请求
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先验证一下用户的登陆状态
        //如果未登陆,就要求用户先登陆
        HttpSession session = req.getSession(false);
        if (session == null){
            //用户未登陆
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("请先登陆!!!");
            return;
        }
        //已经登陆成功.
        //取出之前的attribute
        String username = (String) session.getAttribute("username");
        Long time = (Long) session.getAttribute("time");
        System.out.println("username="+username + ", time= "+time);

        //根据这样的内容去构造页面.
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("欢迎您, "+ username + "! 上次登陆时间: "+time);
    }
}
